package math.组合数学;

import java.util.stream.IntStream;

public class 组合数 {

    // 打表
    class CombinatorialNumberTable{
        int n;
        int mod;
        // 从i选j
        long[][] C;

        private CombinatorialNumberTable(int n, int mod) {
            this.n = n;
            this.mod = mod;
            C = new long[n+1][n+1];
        }

        public CombinatorialNumberTable of(int n) {
            return new CombinatorialNumberTable(n, Integer.MAX_VALUE);
        }
        public CombinatorialNumberTable of(int n,int mod) {
            return new CombinatorialNumberTable(n, mod);
        }
        private void init() {
            for (int i = 0; i <= n; i++) {
                C[i][i] = C[i][0] = 1;
            }
            for (int i = 1; i <= n; i++) {
                for (int j = i+1; j <= n; j++) {
                    C[j][i] = (C[j-1][i-1]+C[j-1][i])%this.mod;
                }
            }
        }
        //从n选m
        public long getC(int n,int m) {
            if (n > this.n) {
                throw new RuntimeException(String.format("给定的n:%s,m:%s超过范围,maxN:%s", n,m,this,n));
            }
            return C[n][m];
        }
    }
}
